/*
 * Copyright (C) 2015-2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-core/specific/model/planet_life.h"
#include "PlanetWars2dRT-core/specific/model/planet_observer.h"


EXTERN_C_BEGIN


static inline
void
planet_reduce_life_simple(planet_t* a_planet, unsigned int life)
{
  if(a_planet->life_current <= life)
    {
      a_planet->life_current = 0;
    }
  else
    {
      a_planet->life_current -= life;
    }
}

bool
planet_reduce_life_no_notify(planet_t* a_planet, unsigned int life)
{
  if(a_planet->life_current == 0 || life == 0)
    {
      return false;
    }
  planet_reduce_life_simple(a_planet, life);
  return true;
}

bool
planet_reduce_life(planet_t* a_planet, unsigned int life)
{
  if(planet_reduce_life_no_notify(a_planet, life))
    {
      struct observer_data_with_code data;
      data.code = PLANET_OBSERVER_CODE_LIFE_CHANGE;
      data.data = NULL;
      observers_notify(&a_planet->observers, a_planet, &data);
      return true;
    }
  return false;
}


EXTERN_C_END
